//
//  ZPowerChoseVC.m
//  WeClubs
//
//  Created by Zed Link on 4/5/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZPowerChoseVC.h"
#import "ZPowerCell.h"
#import "ZCommentHeader.h"

NSString * const PowerChoseCellReuseID = @"ZPowerChoseCellReuseID";
NSString * const PowerChoseHeaderReuseID = @"ZPowerChoseHeaderReuseID";

@interface ZPowerChoseVC () <ZPowerCellDelegate>

/** 数据源 */
@property (strong, nonatomic) NSArray <NSDictionary *> *dataSource;

/** 选中的权限 */
@property (strong, nonatomic) NSMutableArray <NSDictionary *> *selectedPowers;

@end

@implementation ZPowerChoseVC

+ (instancetype)zNew
{
    return [self zNewGroup];
}

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"权限选择";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"完成" andSelector:@selector(finishBtnAction)];
}

- (void)finishBtnAction
{
    if ([self.delegate respondsToSelector:@selector(powerChoseControllerDidSelectPowers:)]) {
        [self.delegate powerChoseControllerDidSelectPowers:self.selectedPowers];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - View Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 52;
    self.tableView.separatorColor = SEPARATOR_BG_COLOR;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZPowerCell" bundle:nil] forCellReuseIdentifier:PowerChoseCellReuseID];
    [self.tableView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:PowerChoseHeaderReuseID];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data = self.dataSource[indexPath.row];
    ZPowerCell *cell = [tableView dequeueReusableCellWithIdentifier:PowerChoseCellReuseID];
    cell.delegate = self;
    [cell setPowerName:data[PowerNameKey] andDescription:data[PowerDescKey]];
    cell.state = [data[PowerSelectedKey] unsignedIntegerValue];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:PowerChoseHeaderReuseID];
    header.alignment = ZTableHeaderAlignmentCenter;
    header.style = ZTableHeaderStyleSetting;
    [header zSetTitle:@"权限选择"];
    
    return  header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SETTING_STYLE_HEIGHT;
}

#pragma mark - ZPowerCellDelegate

- (void)powerCell:(ZPowerCell *)cell selectPower:(BOOL)selected withDescription:(NSString *)powerDesc
{
    NSDictionary *tempDict = nil;
    for (NSDictionary *dict in self.dataSource) {
        if ([[dict objectForKey:PowerNameKey] isEqualToString:powerDesc]) {
            tempDict = dict;
        }
    }
    
    if (tempDict != nil) {
        if (selected == YES && ![self.selectedPowers containsObject:tempDict]) {
            [self.selectedPowers addObject:tempDict];
        } else if (selected == NO && [self.selectedPowers containsObject:tempDict]) {
            [self.selectedPowers removeObject:tempDict];
        }
    }
}

#pragma mark - Getter & Setter

- (NSArray *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = @[
                        @{PowerNameKey : @"职位指定",
                          PowerDescKey : @"具备该资格, 可以指定其他成员的职位, 如设定某成员为干部职位等",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(1)},
                        @{PowerNameKey : @"成员变更",
                          PowerDescKey : @"具备该资格, 可以对社团成员进行管理, 如新成员加入, 老成员退出等",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(2)},
                        @{PowerNameKey : @"会议组织",
                          PowerDescKey : @"具备该资格, 可以组织社团会议, 通知参与成员, 统计参与数据等",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(3)},
                        @{PowerNameKey : @"活动发起",
                          PowerDescKey : @"具备该资格, 可以发起活动, 包括社团活动与校园活动",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(4)},
                        @{PowerNameKey : @"任务指派",
                          PowerDescKey : @"具备该资格, 可以向社团成员指派新的任务, 了解任务的完成情况等",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(5)},
                        @{PowerNameKey : @"相册调整",
                          PowerDescKey : @"具备该资格, 可以对社团的相册或相册中的照片进行调整修改",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(6)},
                        @{PowerNameKey : @"资料变更",
                          PowerDescKey : @"具备该资格, 可以对社团的信息进行编辑修改, 如社团介绍, 社团荣誉等",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(7)},
                        @{PowerNameKey : @"发起群聊",
                          PowerDescKey : @"具备该资格, 可以选择社团中的部分成员, 建立新的群聊",
                          PowerSelectedKey : @(0),
                          PowerIndexKey : @(8)},
                        ];
    }
    return _dataSource;
}

- (NSMutableArray<NSDictionary *> *)selectedPowers
{
    if (_selectedPowers == nil) {
        _selectedPowers = [NSMutableArray array];
    }
    return _selectedPowers;
}

@end
